// 转变成$格式
export function numberFormat(num) {
  const list = (num + "").split(".")
  const numb = list[0]
  const numbers = numb.toString().split('').reverse()
  const segs = []
  while (numbers.length) segs.push(numbers.splice(0, 3).join(''))
  let val = segs.join(',').split('').reverse().join('')
  if (list.length > 1) {
    return `${val}.${list[1]}`
  } else {
    return val
  }
}

// 随机生成颜色值
export function randomColor() {
  const r = Math.floor(Math.random() * 256)
  const g = Math.floor(Math.random() * 256)
  const b = Math.floor(Math.random() * 256)
  return `rgb(${r}, ${g}, ${b})`
}

// 获取URL参数（已转义）
export function getUrlParamsByName(name) {
  const i = location.href.indexOf("?")
  const afterUrl = i > -1 ? decodeURI(location.href.slice(i+1)) : ""
  const group = afterUrl.split("&").map(r => {
    return r.split("=")
  })
  const cur = group.find(r => r[0] === name)
  let val = ""
  if (cur) {
    const str = cur[1] // 转换 JSON.stringify()
    try {
      val = JSON.parse(str)
    } catch (e) {
      console.warn("getUrlParamsByName Warn: ", e)
      val = str
    }
    return decodeURIComponent(val)
  } else {
    return val
  }
}

export function calcRate() {
  // console.log("*********** windowResize *************", { currentRate, baseProportion, scale })
  console.log("*********** windowResize *************")
  let scale = {
    width: '1',
    height: '1',
  };
  const baseWidth = 1920
  const baseHeight = 1080
  const baseProportion = parseFloat((baseWidth / baseHeight).toFixed(5));
  // const appRef = this.$refs['appRef'];
  const appRef = document.getElementById("app")
  if (!appRef) return;
  // 当前宽高比
  const currentRate = parseFloat((window.innerWidth / window.innerHeight).toFixed(5));
  if (appRef) {
    // if (currentRate > baseProportion && window.innerWidth !== 1920) {
    //   // 表示更宽
    //   scale.width = ((window.innerHeight * baseProportion) / baseWidth).toFixed(5);
    //   scale.height = (window.innerHeight / baseHeight).toFixed(5);
    //   appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
    // } else {
    //   // 表示更高
    //   scale.width = (window.innerWidth / baseWidth).toFixed(5);
    //   scale.height = (window.innerWidth / baseProportion / baseHeight).toFixed(5);
    //   appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
    // }
    scale.width = window.innerWidth / baseWidth
    scale.height = window.innerHeight / baseHeight
    appRef.style.transform = `scale(${scale.width}, ${scale.height}) translate(-50%, -50%)`;
  }

}


let timer = null
export function windowResize () {
  clearTimeout(timer)
  timer = setTimeout(calcRate, 300)
}